package com.example.smartagrisys.controller;

import com.example.smartagrisys.common.Result;
import com.example.smartagrisys.dto.InventoryDTO;
import com.example.smartagrisys.service.InventoryService;
import com.example.smartagrisys.util.SecurityUtil;
import com.example.smartagrisys.vo.InventoryVO;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 通用库存控制器
 */
@Slf4j
@RestController
@RequestMapping("/inventory")
@RequiredArgsConstructor
public class InventoryController {

    private final InventoryService inventoryService;

    /**
     * 获取当前农户的库存列表
     */
    @GetMapping("/my")
    public Result<List<InventoryVO>> getMyInventory() {
        // 获取当前登录用户ID
        Long currentUserId = SecurityUtil.getCurrentUserId();
        if (currentUserId == null) {
            return Result.error("用户未登录");
        }
        
        List<InventoryVO> inventoryList = inventoryService.getInventoryByFarmerId(currentUserId);
        return Result.success(inventoryList);
    }

    /**
     * 获取当前农户某类型的库存
     */
    @GetMapping("/my/type/{type}")
    public Result<List<InventoryVO>> getMyInventoryByType(@PathVariable String type) {
        // 获取当前登录用户ID
        Long currentUserId = SecurityUtil.getCurrentUserId();
        if (currentUserId == null) {
            return Result.error("用户未登录");
        }
        
        List<InventoryVO> inventoryList = inventoryService.getInventoryByType(currentUserId, type);
        return Result.success(inventoryList);
    }

    /**
     * 获取当前农户低于预警阈值的库存
     */
    @GetMapping("/my/low")
    public Result<List<InventoryVO>> getMyLowInventory() {
        // 获取当前登录用户ID
        Long currentUserId = SecurityUtil.getCurrentUserId();
        if (currentUserId == null) {
            return Result.error("用户未登录");
        }
        
        List<InventoryVO> inventoryList = inventoryService.getLowInventory(currentUserId);
        return Result.success(inventoryList);
    }

    /**
     * 添加库存
     */
    @PostMapping
    public Result<Long> addInventory(@RequestBody InventoryDTO dto) {
        // 获取当前登录用户ID
        Long currentUserId = SecurityUtil.getCurrentUserId();
        if (currentUserId == null) {
            return Result.error("用户未登录");
        }
        
        Long id = inventoryService.addInventory(currentUserId, dto);
        return Result.success(id);
    }

    /**
     * 更新库存
     */
    @PutMapping("/{id}")
    public Result<Boolean> updateInventory(@PathVariable Long id, @RequestBody InventoryDTO dto) {
        // 获取当前登录用户ID
        Long currentUserId = SecurityUtil.getCurrentUserId();
        if (currentUserId == null) {
            return Result.error("用户未登录");
        }
        
        boolean success = inventoryService.updateInventory(currentUserId, id, dto);
        return Result.success(success);
    }

    /**
     * 删除库存
     */
    @DeleteMapping("/{id}")
    public Result<Boolean> deleteInventory(@PathVariable Long id) {
        // 获取当前登录用户ID
        Long currentUserId = SecurityUtil.getCurrentUserId();
        if (currentUserId == null) {
            return Result.error("用户未登录");
        }
        
        boolean success = inventoryService.deleteInventory(currentUserId, id);
        return Result.success(success);
    }

    /**
     * 获取库存详情
     */
    @GetMapping("/{id}")
    public Result<InventoryVO> getInventoryById(@PathVariable Long id) {
        // 获取当前登录用户ID
        Long currentUserId = SecurityUtil.getCurrentUserId();
        if (currentUserId == null) {
            return Result.error("用户未登录");
        }
        
        InventoryVO inventory = inventoryService.getInventoryById(id);
        if (inventory == null) {
            return Result.error("库存记录不存在");
        }
        
        // 检查是否为当前用户的库存
        if (!inventory.getFarmerId().equals(currentUserId)) {
            return Result.error("无权查看此库存记录");
        }
        
        return Result.success(inventory);
    }
} 